#pragma once

#include <iostream>
#include <pthread.h>

namespace MutexModule
{
    class Mutex
    {
    public:
        Mutex(const Mutex&) = delete;
        const Mutex& operator=(const Mutex&) = delete;
        Mutex()
        {
            int n = pthread_mutex_init(&_lock, nullptr);
        }
        void Lock()
        {
            int n = pthread_mutex_lock(&_lock);
        }
        void Unlock()
        {
            int n = pthread_mutex_unlock(&_lock);
        }
        pthread_mutex_t* LockPtr()
        {
            return &_lock;
        }
        ~Mutex()
        {
            int n = pthread_mutex_destroy(&_lock);
        }
    private:
        pthread_mutex_t _lock;
    };

    class LockGuard
    {
    public:
        LockGuard(Mutex& mutex)
            :_mutex(mutex)
        {
            _mutex.Lock();
        }
        ~LockGuard()
        {
            _mutex.Unlock();
        }
    private:
        Mutex& _mutex;
    };
}